package javabean;
import entity.student;
import entity.user;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.HashMap;
import java.util.Map;


public class audit {
    public audit(){
    }
    public static void audit_local(String username,String password, HttpServletRequest request, HttpServletResponse response) throws IOException
    {

        int login_flag = 1;
        FileInputStream file = null;
        BufferedReader bf = null;
        HttpSession session = request.getSession(true);
        request.getSession().removeAttribute("flag");
        try {
            file = new FileInputStream("src/main/java/login.txt");
            bf = new BufferedReader(new InputStreamReader(file));
            String line = null;
            Jsonb jsonb = JsonbBuilder.create();
            while ((line = bf.readLine())!= null&&!line.equals("")) {
                if(line==null)break;
                else {
                    user personDTO = jsonb.fromJson(line, user.class);
                    if (personDTO.getUsername().equals(username) && personDTO.getPassword().equals(password)) {
                        login_flag = 0;

                        Map <String, String> login2 = new HashMap <String, String>();
                        login2.put("password", personDTO.getPassword());
                        login2.put("username", personDTO.getUsername());
                        login2.put("email", personDTO.getemail());
                        login2.put("id", personDTO.getStu_number());
                        login2.put("img", personDTO.getImg());
                        session.setAttribute("login2", login2);
                        break;
                    }
                }
            }
        } catch (IOException e) {
            response.sendRedirect("/error.html");
        } finally {
            try {
                file.close();
                bf.close();
            } catch (IOException e) {
                response.sendRedirect("/error.html");
            }
        }
        if(login_flag == 0) {

            response.sendRedirect("/user.jsp");
        }
        else{
            String a="0";
            session.setAttribute("flag",a);
            response.sendRedirect("/index.jsp");
        }


    }
    public static void audit_zhuce(String username,String password,String password2,String email,String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
        int login_flag = 1;
        FileInputStream fis = null;
        BufferedReader br = null;
        request.getSession().removeAttribute("flag");
        if (password.equals(password2)) {
            try {
                fis = new FileInputStream("src/main/java/login.txt");
                br = new BufferedReader(new InputStreamReader(fis));
                String line = null;
                Jsonb jsonb = JsonbBuilder.create();

                while ((line = br.readLine()) != null&&!line.equals("")) {
                    if (line == null) break;
                    else {
                        user personDTO = jsonb.fromJson(line, user.class);
                        if (personDTO.getStu_number().equals(id)) {
                            login_flag = 0;
                            break;
                        }
                    }
                }
            } catch (IOException e) {
                response.sendRedirect("/error.html");
            } finally {
                try {
                    fis.close();
                    br.close();
                } catch (IOException e) {
                    response.sendRedirect("/error.html");
                }
            }

            if (login_flag == 0)
                response.sendRedirect("/error.html");
            else {
                Jsonb jsonb = JsonbBuilder.create();
                user userDto = new user(username, password, email, id, "images/user.png");
                String st = jsonb.toJson(userDto);
                FileWriter fw = null;
                try {
                    File f = new File("src/main/java/login.txt");
                    fw = new FileWriter(f, true);
                } catch (IOException e) {
                    response.sendRedirect("/error.html");
                }
                PrintWriter pw = new PrintWriter(fw);
                pw.println(st);
                pw.flush();
                try {
                    fw.flush();
                    pw.close();
                    fw.close();
                } catch (IOException e) {
                    response.sendRedirect("/error.html");
                }
                response.sendRedirect("/index.jsp");
            }
        }
        else{
            response.sendRedirect("/error.html");
        }
    }






    public static void audit_m(student stu, HttpServletRequest request, HttpServletResponse response) throws IOException {
        FileInputStream fs = null;
        BufferedReader bufferedReader = null;
        int flag = 1;
        try {
            fs = new FileInputStream("src/main/java/login.txt");
            bufferedReader = new BufferedReader(new InputStreamReader(fs));
        } catch (FileNotFoundException e) {
            response.sendRedirect("/error.html");
        }
        String line = null;
        StringBuffer buf = new StringBuffer();
        Jsonb jsonb = JsonbBuilder.create();
        while ((line = bufferedReader.readLine()) != null&&!line.equals("")) {
            if (line == null) break;
            else {
                user personDTO = jsonb.fromJson(line, user.class);
                if (personDTO.getStu_number().equals(stu.getUsername())) {
                    flag = 0;
                    HttpSession session = request.getSession(true);
                    Map <String, String> login2 = new HashMap <String, String>();
                    login2.put("password", personDTO.getPassword());
                    login2.put("username", personDTO.getUsername());
                    login2.put("id", personDTO.getStu_number());
                    login2.put("email", personDTO.getemail());
                    login2.put("img", personDTO.getImg());
                    session.setAttribute("userName", login2);
                    break;
                }
                buf.append(line + '\n');
            }
        }
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("src/main/java/login.txt")));

        } catch (Exception e) {
            response.sendRedirect("/error.html");
        }
        if(flag == 1)
        {
            user userDTO = new user(stu.getName(),"123456","123@qq.com",stu.getUsername(),"images/user.png");
            String str = jsonb.toJson(userDTO);
            buf.append(str + '\n');
        }
        out.write(buf.toString());
        out.close();
        response.sendRedirect("/user.jsp");
    }
}
